package org.genew.squadra.xml.common;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class ParsMod {
//	XMLStreamReader reader;
//	XMLInputFactory inFactory; //= XMLInputFactory.newInstance();
//	XMLEventReader eventReader; //= inFactory.createXMLEventReader(new FileInputStream(file));
//	XMLOutputFactory factory; //= XMLOutputFactory.newInstance();
//	XMLStreamWriter writer; //= factory.createXMLStreamWriter(new FileWriter(file, true));
//	XMLEventWriter  writerEvent;
	
	public void findReadModDom(File file, String element, String attributeName, String attributeValue){

			  
			   File xmlFile = file;  
			   DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();  
			   DocumentBuilder documentBuilder;
			try {
				documentBuilder = documentFactory.newDocumentBuilder();
				Document doc = documentBuilder.parse(xmlFile);
				doc.getDocumentElement().normalize();
				//chiamo la funzione
				updateAttributeValue(doc,element,attributeName,attributeValue);

				//write the updated document to file or console
	            doc.getDocumentElement().normalize();
	            TransformerFactory transformerFactory = TransformerFactory.newInstance();
	            Transformer transformer = transformerFactory.newTransformer();
	            
	            DOMSource source = new DOMSource(doc);
	            StreamResult result = new StreamResult(xmlFile);
	            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
	            transformer.transform(source, result);
	            System.out.println("XML file updated successfully");
			} catch (ParserConfigurationException | SAXException | IOException | TransformerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
			   
			  
	}

	private void updateAttributeValue(Document doc,String element, String attributeName,
			String attributeValue) {
		NodeList attributi = doc.getElementsByTagName(element);
		System.out.println("Elemento in fase di update =>> " + element);
		System.out.println("Elemento in fase di update =>> " + attributi.toString());
        Element emp = null;
        //loop for each employee
        for(int i=0; i<attributi.getLength();i++){
        	
            emp = (Element) attributi.item(i);
            System.out.println("----------item =>> " + attributi.item(i).toString());
            Node attributo = emp.getAttributeNode(attributeName);
            System.out.println("..........Attributo in fase di update =>> " + emp.getAttributeNode(attributeName).toString());
            attributo.setNodeValue(attributeValue);
            System.out.println("..........attributo. =>> " + attributo.toString());
            System.out.println("..........Attributo aggiornato  " + emp.getAttributeNode(attributeName).toString());
        }
		
	}		    

	
	
	
	
	
//	public void findReadMod(File file, String element, String attributeName, String attributeValue){
//		inFactory = XMLInputFactory.newInstance();
//			try {
//				reader  = inFactory.createXMLStreamReader(new FileInputStream(file));
//				//eventReader = inFactory.createXMLEventReader(new FileInputStream(file));
//				
//				factory = XMLOutputFactory.newInstance();
//				writerEvent = factory.createXMLEventWriter(new FileWriter(file + ".copia", true));
//				int eventType = reader.getEventType();
//				//writer = factory.createXMLStreamWriter(new FileWriter(file, true));
//				//writer.writeStartDocument();
//				//writerEvent.add(eventReader);
//				System.out.println("==========================================");
//				System.out.println("=========Lettura e Scrittura XML==========");
//				System.out.println("==========================================");
//				System.out.println("======");
//				while(reader.hasNext()){
//					eventType =  reader.next();
//					switch  (eventType)
//				    {
//				        case XMLEvent.START_ELEMENT:
//				        	System.out.println(reader.getElementText());
//				        	break;
//				        case XMLEvent.END_ELEMENT:
//				        	System.out.println("end element");
//				        	break;
//				        case XMLEvent.PROCESSING_INSTRUCTION:
//				        	System.out.println("PROCESSING_INSTRUCTION");
//				        	break;
//				        case XMLEvent.CHARACTERS:
//				        	System.out.println("CHARACTERS");
//				        	break;
//				        case XMLEvent.COMMENT:
//				        	System.out.println("COMMENT");
//				        	break;
//				        case XMLEvent.START_DOCUMENT:
//				        	System.out.println("START_DOCUMENT");
//				        	break;
//				        case XMLEvent.END_DOCUMENT:
//				        	System.out.println("END_DOCUMENT");
//				        	break;
//				        case XMLEvent.ENTITY_REFERENCE:
//				        	System.out.println("ENTITY_REFERENCE");
//				        	break;
//				        case XMLEvent.ATTRIBUTE:
//				        	System.out.println("ATTRIBUTE" + reader.getAttributeLocalName(eventType));
//				        	break;
//				        case XMLEvent.DTD:
//				        	System.out.println("dtd");
//				        	break;
//				        case XMLEvent.CDATA:
//				        	System.out.println("cadata");
//				        	break;
//				        case XMLEvent.SPACE:
//				        	System.out.println("space");
//				        	break;
//				    }
//				  System.out.println("UNKNOWN_EVENT_TYPE ,   "+ eventType);
//				} 
//				
//			} catch (FileNotFoundException | XMLStreamException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			
//			
//		
//
//	}
}
